package Demo428;



import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ChatServer {

    public static void main(String[] args) {
        int port = 7890;
        ServerSocket server = null;
        int count = 0;
        List<Socket> socketList = Collections.synchronizedList(new ArrayList<Socket>());

        try {
            server = new ServerSocket(port);
            System.out.println("服务已启动！");
            while (true){       //一直监听
                Socket socket = server.accept();
                count++;
                socketList.add(socket);
                String ip = socket.getInetAddress().getHostAddress();
                int remotePort = socket.getPort();
                System.out.println("第"+count+"个"+ip+".."+port+"客户端已成功与客户端连接成功！");
                //为每一个客户端开启一个专门工作线程为之服务
                Thread workThread = new Thread(new ServerHandler(socketList));
                workThread.start();
            }


        } catch (IOException e) {
            e.printStackTrace();
        }


    }

}

